<?php 
class AppinfoAction extends Action{
	public function index(){
		$map=array();
    	//总合数据不准在action重新统计
    	if ($_POST['startdate']&&$_POST['enddate']){//时间间查询
    			$this->date_sum($_POST['startdate'], $_POST['enddate']);	
    	}elseif($_POST['in_pv_ip_uv']){//排序
//    		dump($_SESSION['volist']);
    		foreach ($_SESSION['volist'] as $key => $row) {
			    $volume[$key]  = $row[$_POST['in_pv_ip_uv']];  
			}
			array_multisort($volume, SORT_DESC, $_SESSION['volist']);
    	}elseif($_GET['link_date']){//最近几天数据
    		if($_GET['link_date']==1){//昨天的数据
    			$tmp=time() - 86400*1;
    		}
    		if($_GET['link_date']==7){//7天的数据
    			$tmp=time() - 86400*7;
    		}
    		if($_GET['link_date']==30){//30天的数据
    			$tmp=time() - 86400*30;
    		}
    			$enddate=date("Y-m-d",time());
				$startdate=date("Y-m-d",$tmp);
    		if($_GET['link_date']==1){//昨天的数据
    			$enddate=$startdate;
    		}				
				$this->date_sum($startdate, $enddate);
    	}else {
				$app_sort=3;
				$info_model=M('App_info');
    			if ($_POST['category_select']=="1"){
					$app_sort=1;
					//$map="app_sort=".$app_sort;
					$map['app_sort']=$app_sort;
				}
    			if ($_POST['category_select']=="2"){
					$app_sort=2;
					//$map="app_sort=".$app_sort;
					$map['app_sort']=$app_sort;
				}
		    	if ($_POST['guanjianzi']){
		    		$map['app_name'] = array('like','%'.$_POST['guanjianzi'].'%');	
		    	}

		    	$this->_list($info_model,$map);
		    	
		    	
		    	$time=time();
		    	$app_date_tm=date("Y-m-d",$time);
		    	foreach ($_SESSION['volist'] as $key => $value){					
		    		$app_table_ip="app_table_ip_".$value['id'];
					$app_table_ip_model = M($app_table_ip); 
					$app_table_ip_info=$app_table_ip_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_ip_info){
						$app_table_ip_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_ip_info_aggregate[$nubmer_h]=$app_table_ip_info[0][$nubmer_h];	
							}
							$app_table_ip_info_aggregate_val=array_sum($app_table_ip_info_aggregate);
							if ($app_table_ip_info_aggregate_val!=$app_table_ip_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_ip_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_ip_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['ip']=$app_table_ip_info_aggregate_val;
		    		
		    		$app_table_pv="app_table_pv_".$value['id'];
					$app_table_pv_model = M($app_table_pv); 
					$app_table_pv_info=$app_table_pv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_pv_info){
						$app_table_pv_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_pv_info_aggregate[$nubmer_h]=$app_table_pv_info[0][$nubmer_h];	
							}
							$app_table_pv_info_aggregate_val=array_sum($app_table_pv_info_aggregate);
							if ($app_table_pv_info_aggregate_val!=$app_table_pv_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_pv_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_pv_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['pv']=$app_table_pv_info_aggregate_val;
		    		
		    		$app_table_uv="app_table_uv_".$value['id'];
					$app_table_uv_model = M($app_table_uv); 
					$app_table_uv_info=$app_table_uv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_uv_info){
						$app_table_uv_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_uv_info_aggregate[$nubmer_h]=$app_table_uv_info[0][$nubmer_h];	
							}
							$app_table_uv_info_aggregate_val=array_sum($app_table_uv_info_aggregate);
							if ($app_table_uv_info_aggregate_val!=$app_table_uv_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_uv_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_uv_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['uv']=$app_table_uv_info_aggregate_val;
		    	}	
    	}
    	
    	  foreach ($_SESSION['volist'] as  $row) {
				   		$all_total_pv[]=$row['pv'];
				   		$all_total_ip[]=$row['ip'];
				   		$all_total_uv[]=$row['uv'];
				   		  
					}  
//dump($_SESSION['volist']);
		$this->assign('all_total_pv',array_sum($all_total_pv));
		$this->assign('all_total_ip',array_sum($all_total_ip));
		$this->assign('all_total_uv',array_sum($all_total_uv));
    	$this->assign('list',$_SESSION['volist']);// 赋值数据集
    	$this->assign('app_sort',$app_sort);
    	$this->display();
	}
	
	
	private function date_sum($startdate,$enddate,$map=''){
				$info_model=M('App_info');
    			$this->_list($info_model,$map);
    			$data['app_date'] = array('between', array($startdate, $enddate));
    			foreach ($_SESSION['volist'] as $key => $value){
		    		$app_table_ip="app_table_ip_".$value['id'];
					$app_table_ip_model = M($app_table_ip); 
					$app_table_ip_info=$app_table_ip_model->where($data)->sum('aggregate');
					if (!$app_table_ip_info){
						$app_table_ip_info=0;
					}
					
		    		$_SESSION['volist'][$key]['ip']=$app_table_ip_info;
		    		
		    		$app_table_pv="app_table_pv_".$value['id'];
					$app_table_pv_model = M($app_table_pv); 
					$app_table_pv_info=$app_table_pv_model->where($data)->sum('aggregate');
					if (!$app_table_pv_info){
						$app_table_pv_info=0;
					}
					
		    		$_SESSION['volist'][$key]['pv']=$app_table_pv_info;
		    		
		    		$app_table_uv="app_table_uv_".$value['id'];
					$app_table_uv_model = M($app_table_uv); 
					$app_table_uv_info=$app_table_uv_model->where($data)->sum('aggregate');
					if (!$app_table_uv_info){
						$app_table_uv_info=0;
					}
					
		    		$_SESSION['volist'][$key]['uv']=$app_table_uv_info;
    			}
	}
	
	protected function _list($modelname, $map = '', $field = '*',$asc = false ,$listRows = 100) {
		if (empty($map['app_info_pk'])){$map['app_info_pk']=0;}

		
		$count = $modelname->where($map)->count();
		if($count>0){
			import ( "@.ORG.Page" );
			$sort = $asc ? 'asc' : 'desc';
			$p = new Page ($count, $listRows );
			$page =$p->show();

			$_SESSION['volist']=$voList = $modelname->field($field)->where($map)->order($asc)->limit($p->firstRow . ','.$p->listRows)->select();			
//			dump($voList);
			
 			$this->assign('page',$page);// 赋值分页输出
		}
	}
	
	
	public function applist(){
		
		$map['app_info_pk']=$_GET['app_id'];
		
    	//总合数据不准在action重新统计
    	if ($_POST['startdate']&&$_POST['enddate']){//时间间查询
    			$this->date_sum($_POST['startdate'], $_POST['enddate'], $map);	
    	}elseif($_POST['in_pv_ip_uv']){//排序
//    		dump($_SESSION['volist']);
    		foreach ($_SESSION['volist'] as $key => $row) {
			    $volume[$key]  = $row[$_POST['in_pv_ip_uv']];  
			}
			array_multisort($volume, SORT_DESC, $_SESSION['volist']);
    	}elseif($_GET['link_date']){//最近几天数据
    		if($_GET['link_date']==1){//昨天的数据
    			$tmp=time() - 86400*1;
    		}
    		if($_GET['link_date']==7){//7天的数据
    			$tmp=time() - 86400*7;
    		}
    		if($_GET['link_date']==30){//30天的数据
    			$tmp=time() - 86400*30;
    		}
    			$enddate=date("Y-m-d",time());
				$startdate=date("Y-m-d",$tmp); 
    		if($_GET['link_date']==1){//昨天的数据
    			$enddate=$startdate;
    		}				
				$this->date_sum($startdate, $enddate, $map);
    	}else {
				$app_sort=3;
				$info_model=M('App_info');
    			if ($_POST['category_select']=="1"){
					$app_sort=1;
					//$map="app_sort=".$app_sort;
					$map['app_sort']=$app_sort;
				}
    			if ($_POST['category_select']=="2"){
					$app_sort=2;
					//$map="app_sort=".$app_sort;
					$map['app_sort']=$app_sort;
				}
		    	if ($_POST['guanjianzi']){
		    		$map['app_name'] = array('like','%'.$_POST['guanjianzi'].'%');	
		    	}

		    	$this->_list($info_model,$map);
		    	
		    	
		    	$time=time();
		    	$app_date_tm=date("Y-m-d",$time);
		    	foreach ($_SESSION['volist'] as $key => $value){					
		    		$app_table_ip="app_table_ip_".$value['id'];
					$app_table_ip_model = M($app_table_ip); 
					$app_table_ip_info=$app_table_ip_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_ip_info){
						$app_table_ip_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_ip_info_aggregate[$nubmer_h]=$app_table_ip_info[0][$nubmer_h];	
							}
							$app_table_ip_info_aggregate_val=array_sum($app_table_ip_info_aggregate);
							if ($app_table_ip_info_aggregate_val!=$app_table_ip_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_ip_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_ip_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['ip']=$app_table_ip_info_aggregate_val;
		    		
		    		$app_table_pv="app_table_pv_".$value['id'];
					$app_table_pv_model = M($app_table_pv); 
					$app_table_pv_info=$app_table_pv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_pv_info){
						$app_table_pv_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_pv_info_aggregate[$nubmer_h]=$app_table_pv_info[0][$nubmer_h];	
							}
							$app_table_pv_info_aggregate_val=array_sum($app_table_pv_info_aggregate);
							if ($app_table_pv_info_aggregate_val!=$app_table_pv_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_pv_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_pv_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['pv']=$app_table_pv_info_aggregate_val;
		    		
		    		$app_table_uv="app_table_uv_".$value['id'];
					$app_table_uv_model = M($app_table_uv); 
					$app_table_uv_info=$app_table_uv_model->where("app_date='".$app_date_tm."'")->select();
					if (!$app_table_uv_info){
						$app_table_uv_info_aggregate_val=0;
					}else {
						for($nubmer_h=1;$nubmer_h<25;$nubmer_h++){
								if ($nubmer_h<10){
									$nubmer_h="0".$nubmer_h;
								}
								$app_table_uv_info_aggregate[$nubmer_h]=$app_table_uv_info[0][$nubmer_h];	
							}
							$app_table_uv_info_aggregate_val=array_sum($app_table_uv_info_aggregate);
							if ($app_table_uv_info_aggregate_val!=$app_table_uv_info[0]['aggregate']){
								$app_table_data['aggregate'] = $app_table_uv_info_aggregate_val;
								$app_table_map['app_date']=$app_date_tm;
								$app_table_uv_model->where($app_table_map)->save($app_table_data);
							}
					}
		    		$_SESSION['volist'][$key]['uv']=$app_table_uv_info_aggregate_val;
		    	}	
    	}
    	
    	  foreach ($_SESSION['volist'] as  $row) {
				   		$all_total_pv[]=$row['pv'];
				   		$all_total_ip[]=$row['ip'];
				   		$all_total_uv[]=$row['uv'];
				   		  
					}  
//dump($_SESSION['volist']);
		$this->assign('app_info_pk',$map['app_info_pk']);
		$this->assign('all_total_pv',array_sum($all_total_pv));
		$this->assign('all_total_ip',array_sum($all_total_ip));
		$this->assign('all_total_uv',array_sum($all_total_uv));
    	$this->assign('list',$_SESSION['volist']);// 赋值数据集
    	$this->assign('app_sort',$app_sort);
    	$this->display();
		
	}
}
?>